package
{
	import com.bit101.components.Style;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.XMLLoader;
	import com.rover022.flixelgame.GameConfig;
	import com.rover022.flixelgame.event.ItemEvent;
	import com.rover022.flixelgame.manager.ItemManager;
	import com.rover022.flixelgame.ui.equipupgrade.GeneralityGridUI;
	import com.rover022.flixelgame.vo.ItemVo;
	import com.rover022.tool.view.ComposePane;
	import com.rover022.tool.view.ControlPane;
	import com.rover022.tool.view.ImageGallery;
	import com.rover022.tool.view.object.ItemUI;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import ghostcat.manager.DragManager;
	import ghostcat.manager.RootManager;
	import ghostcat.ui.ToolTipSprite;

	[SWF(width = "1200", height = "820")]
	/**
	 *
	 * @author flashpig
	 */
	public class ItemEditProject extends Sprite
	{
		public var controlpane:ControlPane;
		public var imageContain:ImageGallery;
		private var _composePane:ComposePane = new ComposePane();

		public function ItemEditProject()
		{
			Style.setStyle(Style.DARK);
			Style.fontSize = 12;
			Style.embedFonts = false;
			RootManager.register(this);
			controlpane = new ControlPane(this);
			controlpane.y = 800;
			addChild(controlpane);
			imageContain = new ImageGallery();
			addChild(imageContain);
			addEventListener(Event.ADDED_TO_STAGE, addStageHandle);
			addEventListener(ItemEvent.ITEM_SELECT, itemSelectHandle);
		}

		protected function itemSelectHandle(event:Event):void
		{
			var item:ItemUI = event.target as ItemUI;
			//判断一下容器可以装吗?
			
			//可以装才执行下面的
			if (item.selected)
			{
				var copyItem:DisplayObject = item.getCopy();
				_composePane.addBox(copyItem);
			}
		}

		protected function addStageHandle(event:Event):void
		{
			stage.addChild(new ToolTipSprite());
		}

		//支持的功能		
		/**
		 * 导入
		 */
		public function importInFile():void
		{
			var xmlLoader:XMLLoader = new XMLLoader("item.xml");
			xmlLoader.addEventListener(LoaderEvent.COMPLETE, xmlComplete);
			xmlLoader.load();
		}

		protected function xmlComplete(event:LoaderEvent):void
		{
			trace("xmlComplete")
			var xmlLoader:XMLLoader = event.currentTarget as XMLLoader;
			GameConfig.itemData = xmlLoader.getContent("item.xml");
			ItemManager.getInstance().makeItemDictionary();
			ItemManager.getInstance().makeItemRelationship();
			for (var i:int = 0; i < GameConfig.itemData.RECORD.length(); i++)
			{
				var id:String = GameConfig.itemData.RECORD[i].@id;
				var itemvo:ItemVo = ItemManager.getInstance().getItemFromConfig(id);
				var grid:GeneralityGridUI = ItemManager.getInstance().makeGeneralityGrid(itemvo);
				var _itemui:ItemUI = new ItemUI(grid);
				_itemui.itemVo = itemvo;
				_itemui.addEventListener(MouseEvent.CLICK, itemClick);
				DragManager.register(_itemui);
				addGridToContain(_itemui)
			}
			sortBox();
		}

		protected function itemClick(event:MouseEvent):void
		{
			if(!_composePane.canAugment()){
				return ;
			}
			var item:ItemUI = event.currentTarget as ItemUI;
			item.selected = !item.selected;
		}

		public function addGridToContain(grid:DisplayObject):void
		{
			// TODO Auto Generated method stub
			imageContain.itemContainer.addChild(grid);
		}

		public function sortBox():void
		{
			var size:int = 8;
			for (var i:int = 0; i < imageContain.itemContainer.numChildren; i++)
			{
				var display:DisplayObject = imageContain.itemContainer.getChildAt(i);
				display.x = 100 * (i % size);
				display.y = 100 * int(i / size);
			}
		}

		/**
		 * 导出
		 */
		public function importOutFile():XML
		{
			return null
		}

		public function showComposePane():void
		{
			if (_composePane == null)
			{
				_composePane
			}
			addChild(_composePane);
		}
	}
}
